package com.example.rumen;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Albums extends AppCompatActivity implements View.OnClickListener {

    private ImageView image_view;
    private TextView txt;
    private Button btn_pre;
    private View btn_next;
    private int[] images;
    private String[] names;
    private int btn_id;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_albums);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        /*
         * 1.初始化界面
         * */
        initView();

        /*
         * 2.初始化数据
         * */
        initData();

        btn_pre.setOnClickListener(this);
        btn_next.setOnClickListener(this);


    }


    private void initData() {
        images = new int[]{R.drawable.jw, R.drawable.touxiang, R.drawable.hana, R.drawable.jinse, R.drawable.damao};
        names = new String[]{"第一张", "第二张", "第三张", "第四张", "第五张"};
        image_view.setImageResource(images[0]);
        txt.setText(names[0]);

    }

    private void initView() {
        image_view = findViewById(R.id.imageView);
        txt = findViewById(R.id.textView);
        btn_pre = findViewById(R.id.button_previous);
        btn_next = findViewById(R.id.button_next);
        index = 0;

    }

    @Override
    public void onClick(View v) {
        btn_id = v.getId();
        if (btn_id == R.id.button_previous) {
            //如果是第一张，点击上一张不能返回最后一张,并提示信息"已经是第一张图片"

            if (index == 0) {
                String msg = "已经是第一张图片";
                Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_SHORT).show();
//                txt.setText(msg);
//                index = images.length - 1;
            } else {
                index--;
            }
        }

        if (btn_id == R.id.button_next) {
            if (index == images.length - 1) {
                String msg = "已经是最后一张图片";
                Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_SHORT).show();
//                txt.setText(msg);
//                index = 0;
            } else {
                index++;
            }
        }

        upDateView();

    }

    private void upDateView() {
        image_view.setImageResource(images[index]);
        txt.setText(names[index]);
    }
}